//
//  StudyPainCollectCell.swift
//  Auscultation
//
//  Created by 张文博 on 2021/3/21.
//

import UIKit

protocol StudyPainCollectCellDelegate: class {
    func selectStartStudyBtnIndex(_ model: LGFImageTitleView)
    func selectContinueStudyBtnIndex(_ model: LGFImageTitleView)

}

class StudyPainCollectCell: UICollectionViewCell {
    @IBOutlet weak var coverView: UIView!
    @IBOutlet weak var studyBtn: UIButton!

    @IBOutlet weak var testBtn: UIButton!
    var model:CourseStudyModel = CourseStudyModel() {
        didSet{
            nameL.text = model.title
            bodyL.text = model.typeString ?? ""
            bottomNameL.text = model.title
            bottomDesL.text = model.presentation
            bottomTimeL.text = model.typeString ?? "" + "丨" + model.time + "分钟"

            processView.title = "\(model.stuRate * 100)" + "%"
            processView.setProgress(Float(model.stuRate), animated: true, withDuration: 0.5)
            // 禁止学习
            if model.status == "0" {
                studyBtn.isUserInteractionEnabled = false
                testBtn.isUserInteractionEnabled = false
                coverView.isHidden = false
                studyBtn.alpha = 0.4
                testBtn.alpha = 0.4

            // 开始学习
            }else if model.status == "10"{
                studyBtn.isUserInteractionEnabled = true
                testBtn.isUserInteractionEnabled = false
                studyBtn.alpha = 1
                testBtn.alpha = 0.4
                coverView.isHidden = true

                // 学习完成
            }else if model.status == "20" {
                studyBtn.isUserInteractionEnabled = true
                testBtn.isUserInteractionEnabled = true
                studyBtn.alpha = 1
                testBtn.alpha = 1
                coverView.isHidden = true
            }
            
            if model.examStatus == "10" {
                testBtn.isUserInteractionEnabled = true
                testBtn.alpha = 1
            }else if model.examStatus == "0"{
                testBtn.isUserInteractionEnabled = false
                testBtn.alpha = 0.4
            }
            
            if model.eduNum > 0 {
                studyBtn.isUserInteractionEnabled = true
                testBtn.isUserInteractionEnabled = true
                studyBtn.alpha = 1
                testBtn.alpha = 1
                coverView.isHidden = true
            }

        }
    }
    
    weak var delegate: StudyPainCollectCellDelegate?

    @IBOutlet weak var backImageView: UIImageView!
    @IBOutlet weak var topView: UIView!
    
    @IBOutlet weak var processView: CircleProgressView!
    @IBOutlet weak var nameL: UILabel!
    @IBOutlet weak var bodyL: UILabel!
    
    @IBOutlet weak var startStydyBtn: UIButton!
    
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var bottomNameL: UILabel!
    @IBOutlet weak var bottomDesL: UILabel!
    @IBOutlet weak var bottomTimeL: UILabel!
    @IBAction func startStudyClick(_ sender: UIButton) {
        print("开始学习")
//        delegate?.selectStartStudyBtnIndex(nil)
        
        
    }
    
    @IBAction func continueBtnClick(_ sender: UIButton) {
        print("学习")
        studyNum()
        if model.type == "0" {
            let examStart = Date().lgf_TimeStampString()
            let u3d = U3DVC()
            u3d.studyCompeleCourse = {
                self.model.eduNum = self.model.eduNum + 1
                let examEnd = Date().lgf_TimeStampString()
                var studyTime = (examEnd.int! - examStart.int!)/1000 / 60
                studyTime += 1
                self.saveStudyTime(studyTime: studyTime.string)

            }
            
        self.lgf_ViewController?.navigationController?.pushViewController(u3d, animated: true)

        }else{
            let vc = WebVC()
            vc.titleString = model.title
            vc.url = EWEUrl.File_URL + model.filePath
            self.lgf_ViewController?.navigationController?.pushViewController(vc, animated: true)
        }

        

    }
    @IBAction func testClick(_ sender: UIButton) {
        let vc = CourseMoreStudyTestVC()
        vc.stuId = model.id
        self.lgf_ViewController?.navigationController?.pushViewController(vc, animated: true)

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
//        backImageView.snp.makeConstraints { (make) in
//            make.left.bottom.right.right.equalTo(contentView)
//        }
        backImageView.contentMode = .scaleAspectFit
        backImageView.isUserInteractionEnabled = true
//        bgLayer1.frame = bottomView.bounds
//        bgLayer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
//        bottomView.layer.insertSublayer(bgLayer1, at: 0)
//        topView.backgroundColor = .red
        topView.backgroundColor = .clear
        bottomView.backgroundColor = .white
        bottomView.layer.cornerRadius = 5
        bottomView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.15).cgColor
        bottomView.layer.shadowOffset = CGSize(width: 0, height: 1.5)
        bottomView.layer.shadowOpacity = 1
        bottomView.layer.shadowRadius = 5
        backImageView.image = UIImage(named: "学习计划背景")
        processView.backgroundColor = .clear
        processView.lineWidth = 3.0
        processView.trackColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0.3)
        processView.progressColor = UIColor.white
        
        
        processView.isHidden = true
        startStydyBtn.isHidden = true
        testBtn.layer.cornerRadius = 15
        testBtn.layer.masksToBounds = true
        studyBtn.layer.cornerRadius = 15
        studyBtn.layer.masksToBounds = true
        coverView.isHidden = true

    }
    
    
    func studyNum() {
        let paramet = ["eduNum":self.model.eduNum.description]
        guard let userId = EWEUser.lgf.userId else { return  }
        EWERequest.lgf("3dStudy/" + "\(self.model.id)" + "/user/" + userId, paramet, .put,JSONEncoding.default) {  (data) in

            
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }
    
    func saveStudyTime(studyTime:String) {
        let paramet = ["studyTime":studyTime]
        
//        guard let userId = EWEUser.lgf.userId else { return  }
        EWERequest.lgf("eduRecord/", paramet, .post,JSONEncoding.default) {  (data) in

        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }


}
